/**   
 * @Title: WxShopIntercept.java
 * @Package com.wx.interceptor
 * @Description: 
 * @author wangxiao
 * @date 2017年12月25日 下午3:45:05
 */
package com.wx.interceptor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.Easy;
import com.eova.common.utils.time.TimestampUtil;
import com.xiaoleilu.hutool.json.JSONUtil;

/**   
 * @ClassName: WxShopIntercept
 * @Description: 
 * @author wangxiao
 * @date 2017年12月25日 下午3:45:05
 */
public class WxShopIntercept extends MetaObjectIntercept{

	
	private static String GET_ADD_XY = "http://apis.map.qq.com/ws/geocoder/v1/?address=【ADD】&output=json&key=【KEY】";
	
	private static String GET_ADD_KEY ="WPDBZ-DX3CD-WEY4I-HH5Z6-TP2VZ-6CBPX";
    /**
	 * 新增前置任务(事务内)
	 * 
	 * <pre>
	 * ac.record 当前操作数据
	 * -------------
	 * 用法：自动赋值
	 * ac.record.set("create_time", TimestampUtil.getNow());
	 * ac.record.set("create_uid", ac.UID);
	 *
	 * 用法：唯一值判定
	 * int count = Db.queryInt("select count(*) from users where name = ?", ac.record.getStr("name"));
	 * if (count > 0) {
	 *     return Easy.error("名字不能重复");
	 * }
	 * </pre>
	 */
	@Override
    public String addBefore(AopContext ac) throws Exception {
		//获取省市区 地址
		String address = ac.ctrl.getPara("pro")+ac.ctrl.getPara("city")+ac.ctrl.getPara("dis")+ac.ctrl.getPara("address");
		String url = WxShopIntercept.GET_ADD_XY.replace("【ADD】", address).replace("【KEY】", WxShopIntercept.GET_ADD_KEY);
		String response = sendPostRequest(url);
		Map  resMap= JSONUtil.parseObj(response);
		if((int)resMap.get("status")==0){
			//百度获取坐标成功时
			Map mm = (Map) resMap.get("result");
			Map mmm = (Map) mm.get("location");
			ac.record.set("LNG", mmm.get("lng"));
			ac.record.set("LAT", mmm.get("lat"));
			ac.record.set("ACCOUNT_ID", "9999");
			ac.record.set("CREATE_DATE", TimestampUtil.getNow());
		}else{
			return Easy.error("未获取到坐标轴");
		}
        return null;
    }
	@Override
	public String updateBefore(AopContext ac) throws Exception {
		//获取省市区 地址
		String address = ac.ctrl.getPara("pro")+ac.ctrl.getPara("city")+ac.ctrl.getPara("dis")+ac.ctrl.getPara("address");
		String url = WxShopIntercept.GET_ADD_XY.replace("【ADD】", address).replace("【KEY】", WxShopIntercept.GET_ADD_KEY);
		String response = sendPostRequest(url);
		Map  resMap= JSONUtil.parseObj(response);
		if((int)resMap.get("status")==0){
			//百度获取坐标成功时
			Map mm = (Map) resMap.get("result");
			Map mmm = (Map) mm.get("location");
			ac.record.set("lng", mmm.get("lng"));
			ac.record.set("lat", mmm.get("lat"));
			ac.record.set("create_date", TimestampUtil.getNow());
		}else{
			return Easy.error("未获取到坐标轴");
		}
        return null;
	}
	public static void main(String[] args) throws UnsupportedEncodingException {
		
		// TODO Auto-generated method stub
		String address = "上海闵行区罗阳路168号B座501";
		String url_bd="http://api.map.baidu.com/geocoder/v2/?address=【ADD】&output=json&ak=【AK】&ret_coordtype=gcj02ll";
		String url = url_bd.replace("【ADD】",address).replace("【AK】", "jor60Bzq9BV949kIzIakYSya");
//		String out =WxShopIntercept.sendPostRequest("http://apis.map.qq.com/ws/geocoder/v1/?address=北京市海淀区彩和坊路海淀西大街74号&key=WPDBZ-DX3CD-WEY4I-HH5Z6-TP2VZ-6CBPX");

//		System.out.println(out);
	}
	
	public String sendPostRequest(String url) {
		StringBuffer stringBuffer = new StringBuffer("");
		BufferedReader reader = null;
		HttpURLConnection connection = null;
		try {
			URL postUrl = new URL(url);
			connection = (HttpURLConnection) postUrl.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("GET");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setReadTimeout(5000);  
			connection.setConnectTimeout(5000); 
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			
			reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = reader.readLine()) != null) {
			stringBuffer.append(line);
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			connection.disconnect();
			if(reader !=null){
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return stringBuffer.toString();
	}
}
